#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : bigDataProject 
@File    : common_control.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/11/11 15:30 
@Describe: 通用工具类
"""
import json
import random
import string
from typing import Any, Text

import jsonpath


def get_json_result(obj: Any, expr: Text) -> Any:
    """
    提取JSON指定内容
    :param obj: JSON内容
    :param expr: 提取操作符
    :return: 提取内容
    """
    if type(obj) == str:
        responses = json.loads(obj)
    else:
        # 先把传入的值转换为json格式
        responses = json.loads(obj.content)
    # 将需要提取的值提取出来
    result = jsonpath.jsonpath(responses, "{}".format(expr))
    if result:
        if len(result) == 1:
            return result[0]
        return result


def get_json_value(json_obj, key) -> Text:
    """
    获取json对应值
    :param json_obj: json字符串
    :param key: 键
    :return:
    """
    value = dict(json_obj)  # 转化成字典类型
    return value[key]


def get_shop_code(brand_code: Text) -> Text:
    """
    根据品牌编码获取测试门店code
    :param brand_code: 品牌编码
    :return:
    """
    match brand_code:
        case "823882":
            return "5999"
        case "823884":
            return "8499"
        case "823885":
            return "8999"


def get_request_id(length=14):
    """
    生成request_id
    :param length: 随机数长度
    :return:
    """
    request_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
    return request_id


if __name__ == '__main__':
    print(get_request_id())
